<?php

namespace Tbqphplib\Article\Controllers\Admin;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Tbqphplib\Article\Form;
use Tbqphplib\Article\Model\Article;

class ArticleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article());

        $grid->column('id', __('Id'))->sortable();
        $grid->column('title', __('Title'));
        $grid->column('uuid', __('Uuid'));
        $grid->column('keywords', __('标签'))->display(function () {
            $rt = [];
            foreach ($this->keywords as $key => $value) {
                array_push($rt, $value['name']);
            }
            return implode(',', $rt);
        });
        // $grid->column('content', __('Content'));
        // $grid->column('created_at', __('Created at'));
        // $grid->column('created_by', __('Created by'));
        $grid->column('updated_at', '更新时间')->date('Y-m-d H:i')->sortable();
        // $grid->column('updated_by', __('Updated by'));
        $grid->model()->orderBy('id', 'desc');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('content', __('Content'));
        $show->field('created_at', __('Created at'));
        $show->field('created_by', __('Created by'));
        $show->field('updated_at', __('Updated at'));
        $show->field('updated_by', __('Updated by'));
        $show->field('uuid', __('Uuid'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article());

        $form->text('title', __('标题'));
        $form->ckeditor('content', __('内容'));
        $form->text('keywords', '标签');
        // $form->number('created_by', __('Created by'));
        // $form->number('updated_by', __('Updated by'));
        // $form->text('uuid', __('Uuid'));

        return $form;
    }
}
